package com.qg.lizhanqi.videosquare.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qg.lizhanqi.videosquare.R;
import com.qg.lizhanqi.videosquare.bean.VideoInfors;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;
import com.yolanda.nohttp.rest.SimpleResponseListener;

public class CommentActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText edit_comment;
    private TextView tv_CommentableSize;
    private Button button;
    private final String path = "http://101.200.142.201:8080/VideoPlay/comment";
    private RequestQueue requestQueues;
    final String LOGININFO = "loginInfo";
    private VideoInfors.VideoListBean video;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        video = (VideoInfors.VideoListBean) getIntent().getSerializableExtra("video");
        initView();
    }
    private void initView() {
        //第一步：创建Nohttp请求对列
        requestQueues = NoHttp.newRequestQueue();
        edit_comment = (EditText) findViewById(R.id.edit_comment);
        edit_comment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                int length = edit_comment.getText().length();
                tv_CommentableSize.setText(length+"/100");
            }
        });
        tv_CommentableSize = (TextView) findViewById(R.id.tv_CommentableSize);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                submit();
                String userid = getSharedPreferences(LOGININFO, Context.MODE_PRIVATE).getString("userid",null);
                submitComment(path,userid,video.getVideoid(),edit_comment.getText().toString().trim());
                break;
        }
    }
    public void submitComment(String path,String commentuserid,String commentvideo,String commentconent) {
        if (commentuserid==null){
            Toast.makeText(this,"未登录",Toast.LENGTH_LONG).show();
            return;
        }

        Request<String> stringRequest = NoHttp.createStringRequest(path, RequestMethod.POST);
        stringRequest.add("commentuserid",commentuserid);
        stringRequest.add("commentvideo",commentvideo);
        stringRequest.add("commentconent",commentconent);
        requestQueues.add(6969, stringRequest, new SimpleResponseListener<String>() {
            @Override
            public void onSucceed(int i, Response<String> response) {
                String str = response.get();
                JSONObject jsonObject = JSON.parseObject(str);
                String code = jsonObject.getString("code");
                if(code.equals("00000")){
                    finish();

                    Toast.makeText(CommentActivity.this,"评论成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(CommentActivity.this,"评论错误,请稍后重试",Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {
                Toast.makeText(CommentActivity.this,"请重试",Toast.LENGTH_LONG).show();
            }
        });
    }
    //校验内容是否为空
    private void submit() {
        String comment = edit_comment.getText().toString().trim();
        if (TextUtils.isEmpty(comment)) {
            Toast.makeText(this, "评论内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
    }
}
